package api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mysql.jdbc.Connection;
import lombok.SneakyThrows;
import model.Article;
import model.UserInfo;
import util.AppVar;
import util.DBUtils;
import util.ResultUtils;
import util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author shkstart
 * 获取文章详情
 * @create 2023-01-10-23:09
 */
@WebServlet("/myartlist")
public class MyArticleListServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Article> list = new ArrayList<>();
        String msg = "位置参数MyArticleList";
//        String uidstr = req.getParameter("uid");
//        if (!StrUtil.isBlank(uidstr)) { 存在安全隐患
//            int uid = Integer.valueOf(uidstr);

        //验证当前的登录用户
        UserInfo userInfo = SessionUtils.getLoginUser(req);
        if (userInfo != null) {


            int uid = userInfo.getUid();
            //根据Id从数据库中查找文章列表
            //1.得到connection
            Connection connection = DBUtils.getConnection();
            //2.拼接Sql，得到执行器
            String sql = "select * from article where uid =? ";
            PreparedStatement statement = connection.prepareStatement(sql);
            //3.执行执行器，得到结果
            statement.setInt(1, uid);

            ResultSet resultSet = statement.executeQuery();
            //4.拼接article
            while (resultSet.next()) {
                Article article = new Article();
                article.setAid(resultSet.getInt("aid"));
                article.setTitle(resultSet.getString("title"));
                String context = resultSet.getString("context");
                article.setContext(context);
                //简介，取正文的前二十  小于二十取最小
                int subLength = Math.min(20, context.length());
                article.setDesc(context.substring(0, subLength).replace("#", ""));

                String createtime = resultSet.getString("createtime");
                article.setCreatetime(
                        createtime.substring(0, createtime.lastIndexOf("."))
                );
                article.setUpdatetime(resultSet.getString("updatetime"));
                article.setReadcount(resultSet.getInt("readcount"));
                article.setUid(resultSet.getInt("uid"));
                article.setState(resultSet.getInt("state"));
                list.add(article);
            }
            msg = "成功抵达MyArticleList";
        } else {
            msg = "当前用户未登录";
        }
        resp.setContentType(AppVar.RESP_JSON_UTF8);
        resp.getWriter().println(JSONUtil.toJsonStr(
                ResultUtils.toResult(200, list, msg)
        ));
    }
}
